$.ajax({
	url: "./js/music.json",
	type: "get",
	dataType: "json",
	success: function(res) {
		res.sort(function() {
			return Math.random() - 0.5;
		})

		var first_arr = [];
		var second_arr = [];
		for(var i = 0; i < res.length; i++) {
			if(i < 15) {
				//第一批资源路径添加到数组中
				first_arr.push(res[i].audio);
			} else {
				second_arr.push(res[i].audio);
			}
		}

		//预加载
		load1(first_arr, second_arr);
	}
})

function load1(data1, data2) {
	var queue = new createjs.LoadQueue();
	queue.loadManifest(data1);
	queue.on("progress", function(event) {
		var progress = (event.progress * 100).toFixed(0) + "%";
		$("#percentage>span").html(progress);

	})
	queue.on("complete", function() {
		console.log("开始游戏");

		load2(data2);
		$("#loadhide").hide();

	})
	queue.load();
}

function load2(data2) {
	var queue = new createjs.LoadQueue();
	queue.loadManifest(data2);
	queue.on("complete", function() {
		console.log("所有资源加载完毕");
	})
	queue.load();
}
//点击开始
$("#beginbtn").click(function() {
	$("#beginright").css({
		display: "none"
	});
	$("#animation").css({
		display: "block"
	});
	//
	$.ajax({
		url: "./js/music.json",
		type: "get",
		dataType: "json",
		success: function(res) {
			res.sort(function() {
				return Math.random() - 0.5;
			})
			console.log(res[0].item);
			var grade = 0;
			var arr2 = 0;

			function qiequ(arr2) {
				for(i = 0; i < res.length; i++) {
					var res_item = res[i].item;
					var songid = res[arr2].audio;
					$(".audio").eq(i).attr("src", songid);
				}

			}
			var arr1 = 0;

			function qiege(arr1) {
				$(".item").eq(0).html(res[arr1].item[0]);
				$(".item").eq(1).html(res[arr1].item[1]);
				$(".item").eq(2).html(res[arr1].item[2]);

			}
			qiege(0);
			qiequ(0);
			var daan = 0;
			for(let j = 0; j < 3; j++) {
				$(".item").eq(j).click(function(item, index) {
						arr1++;
						arr2++;
						qiege(arr1);
						qiequ(arr2);
						console.log("点击下标",j);
						console.log("答案",res[j].answer)
						if(j == res[j].answer){
							daan++
							console.log("猜对数量",daan);
						}
						$(".guess-text").text("猜对"+daan+" 首")
				})
			}
			

			var first_arr = [];
			var second_arr = [];
			console.log(res)
			for(var i = 0; i < res.length; i++) {
				if(i < 15) {
					//第一批资源路径添加到数组中
					first_arr.push(res[i].audio);
				} else {
					second_arr.push(res[i].audio);
				}
			}

		}
	})
	//倒计时
	function dao(second) {
		var time = document.getElementById("time"); //根据ID名称获取页面元素
		var shuo = setInterval(function() {
			time.innerText = second + "s"; //设置div里面的内容
			//innerText 直接当做内容输出
			//innerHTML 先识别字符串中能作为代码部分，创建元素并加入到页面中
			second--; //次数

			if(second < 0) {
				clearInterval(shuo); //清除定时器
				//					$("#animation").css({display:"none"}); 
			}
		}, 1000)

	}
	dao(59);
	//切换page
	function qie(page) {

		var huan = setInterval(function() {
			var page_width = $(".page").width()
			page++;
			$(".page").eq(page - 1).addClass("xuanzhuan");
			$("#pagebox").css({
				"transform": "translateX(" + -page_width * page + "px" + ")"
			})
			if(page == 6) {
				$("#pagebox").css({
					display: "none"
				});
				$(".itembox").css({
					display: "none"
				});
				$("#flash").css({
					display: "block"
				});
				clearInterval(huan);
				$(".audio")[0].pause();
			}
		}, 10000)

	}
	qie(0);

})
//分享
$("#friend").click(function() {
	$(".ydShare").css({
		display: "block"
	});
	$(".ydShare").click(function() {
		$(".ydShare").css({
			display: "none"
		});
	})
})
//再次挑战
$(".again").click(function() {
	window.location.href = "";
})